"threads";
let baseUrl = 'http://dhh2025.siyme.com'
var storage = storages.create("data");
function httpRequest(url, options, callback) {
    // let headers = options.headers || {}
    // if(myUi && myUi.systemInfo){
    //     console.log('systemInfo' + JSON.stringify(myUi.systemInfo));
    //     console.log('\n deviceInfo' + JSON.stringify(myUi.deviceInfo));
    // }
    console.log('\n url：' + url + '\n request：' + JSON.stringify(options) + '\n');
    let body = options.body || {}
    if (myUi && myUi.systemInfo.token) {
        body['token'] = myUi.systemInfo.token
    }
    if (myUi && myUi.systemInfo.app_id) {
        body['app_id'] = myUi.systemInfo.app_id
    }
    if (myUi && myUi.deviceInfo.mobile_id) {
        body['mobile_id'] = myUi.deviceInfo.mobile_id
    }
    return threads.start(function () {
        let res = http.post(url, body)
        console.log('\n http:res \n', JSON.stringify(body));
        if (res.statusCode == 200) {
            let data = res.body.json()
            console.log('\n 请求结果：\n', JSON.stringify(data) + '\n');
            if (!data.code) {
                callback(data.data)
            } else {
                if(data.code == 401){
                    toast('登录过期，请重新登录')
                    myUi.startInit()
                }else{
                    toast('请求失败：' + data.msg)
                }
                console.log('\n 请求失败：' + JSON.stringify(data));
                callback(null)
            }
        } else {
            callback(null)
            toast('网络请求失败,请稍后再试')
        }
    })
}
module.exports = {
    toLogin: function (data, callback) {
        httpRequest(baseUrl + '/app/auth/login', { body: data }, callback)
    },
    bindDevice: function (params, callback) {
        httpRequest(baseUrl + '/app/app/bindDevice', { body: params }, callback)
    },
    getVersion: function (params, callback) {
        httpRequest(baseUrl + '/app/app/version', { body: params }, callback)
    },
    getTask: function (params, callback) {
        httpRequest(baseUrl + '/app/xhs/task', {}, callback)
    },
    getTask2: function (params, callback) {
        return threads.start(function () {
            // callback({
            //     code: 0,
            //     data: {
            //         taskId: 'send_post',
            //         title: '正常人谁写日记',
            //         info: '这年头了，正常人谁取得日记，谁写得日记',
            //         resource:"https://qiaumi.isimi.net/files/20250114/6785d56b1dd85.png,https://qiamicloud.qiniu.siyme.com/qiamicloud_2025_02_08_67a6b2e15d4df.png,https://qiamicloud.qiniu.siyme.com/qiamicloud_2024_12_13_675bf95cde00e.mp4",
            //     }
            // })
            // callback({
            //     code: 0,
            //     data: {
            //         taskId: 'reply_msg',
            //         title: '正常人谁写日记',
            //         info: '这年头了，正常人谁取得日记，谁写得日记',
            //         resource:"https://video.699pic.com/videos/84/13/36/a_atxeCytxMwyj1614841336.mp4",
            //     }
            // })
            callback({
                code: 0,
                data: {
                    taskId: 'get_post_details',
                    taskIdList: [
                        '96 小红薯DC21215F发布了一篇小红书笔记，快来看吧！ 😆 7GrxsqM4b8UL9Gm 😆 http://xhslink.com/a/kGFZ7j7P3ww5，复制本条信息，打开【小红书】App查看精彩内容！'
                    ]
                }
            })
        });
    },
    putErrorTask: function (params, callback) {
        return threads.start(function () {
            let res = http.get(baseUrl + '/app/xhs/done', params)
            callback(res.statusCode == 200 ? res.body : null)
        });
    },
    doneData: function (body, callback) {
        httpRequest(baseUrl + '/app/xhs/done', { method: 'POST', body: body }, callback)
    }
}
